package com.spider.moudle.api.realize;

import com.alibaba.fastjson.JSONObject;
import com.spider.moudle.constant.Constant;
import com.spider.moudle.util.CenterExecutorUtil;
import com.spider.moudle.util.DateUtil;

import java.io.File;
import java.io.FileWriter;
import java.util.Date;

/**
 * Created by Linbo Hu on 2018/1/8.
 * 爬虫状态的日志记录
 */
public class StandardLogRecorder {

    private static StandardLogRecorder INSTANCE = new StandardLogRecorder();

    /**
     * 计数日志文件的名称
     */
    private final String LOG_FILE_NAME = "count_status_";

    private final JSONObject LOGOBJECT = new JSONObject();

    private final String ADD = "任务生成量";

    private final String TAKE = "任务获取量";

    private final String DATA = "数据抓取量";

    public static StandardLogRecorder getInstance() {
        return INSTANCE;
    }

    private StandardLogRecorder() {
    }

    /**
     * 初始化方法
     */
    public void initialize() {
        CenterExecutorUtil.execute(() -> {
            while (!Thread.interrupted()) {
                File file = new File(LOG_FILE_NAME+ DateUtil.date_to_str(new Date(), DateUtil.yyyy_MM_dd)+".log");
                try {
                    Thread.sleep(Constant.STATUS_INL);
                    JSONObject object = new JSONObject();
                    object.put("detail", LOGOBJECT);
                    object.put("query_time", DateUtil.date_to_str(new Date(), DateUtil.yyyy_MM_dd_HH_mm));
                    System.out.println(object.toJSONString());
                    FileWriter writer = new FileWriter(file, true);
                    writer.write(object.toJSONString()+"\n");
                    writer.flush();
                    writer.close();
                    clear();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }
    /**
     * 添加task计数
     */
    public void addTaskCount(String taskName) {
        plus(taskName, ADD);
    }

    /**
     * 获取任务计数
     * @param taskName
     */
    public void takeTaskCount(String taskName) {
        plus(taskName, TAKE);
    }

    /**
     * 爬虫数据计数
     */
    public void dataCount(String taskName) {
        plus(taskName, DATA);
    }

    /**
     * 根据任务名称和类型计数
     * @param taskName
     * @param type
     */
    private void plus(String taskName, String type) {
        if(LOGOBJECT.containsKey(taskName)) {
            JSONObject object = LOGOBJECT.getJSONObject(taskName);
            if(object.containsKey(type)) {
                Integer count = object.getInteger(type);
                synchronized(object) {
                    object.put(type, ++count);
                }
            } else {
                synchronized(object) {
                    object.put(type, 1);
                }
            }
        } else {
            JSONObject object = new JSONObject();
            object.put(type, 1);
            synchronized(LOGOBJECT) {
                LOGOBJECT.put(taskName, object);
            }
        }
    }

    /**
     * 清除数据
     */
    private void clear() {
        synchronized(LOGOBJECT) {
            LOGOBJECT.forEach((key, value) -> {
                JSONObject object = (JSONObject) value;
                object.entrySet().forEach((item) -> item.setValue(0));
            });
        }
    }


}
